<?php
namespace app\controller;

use app\BaseController;
use app\model\Config;
use app\model\Message;
use think\Request;

/* 配置 */
class MessageController extends BaseController
{
    public function messageList(Request $request)
    {
        $post = $request->param();
        $where = [];
        $list = Message::where($where)
            ->page($post['page'] ?? 1, $request->param('pageSize') ?? 10)->order('update_time desc')->select();
        $count = Message::where($where)->count();
        return pageReturn($list, $request, $count);
    }
    public function addMessage(Request $request)
    {
        $Message = new Message;
        $data = $request->post();
        if ($Message) {
            foreach ($data as $key => $value) {
                $Message->$key = $value;
            }
        }
        $rq = $Message->save();
        if ($rq) {
            return success($request);
        }
    }
    public function updateMessage(Request $request)
    {
        $Message = Message::find($request->post('id'));
        $data = $request->post();
        if ($Message) {
            foreach ($data as $key => $value) {
                $Message->$key = $value;
            }
        }
        $rq = $Message->save();
        if ($rq) {
            return success($request);
        }
    }
    public function delMessage(Request $request)
    {
        $id = $request->param('id');
        $Message = Message::find($id);
        // $rq = $Message->destroy($id);
        $rq = $Message->force()->delete();//直接删除
        if ($rq) {
            return success($request);
        }
    }
}
